<template>
  <div>
    <div class="container-fluid">
      <div class="common_title">
        用户信息修改
      </div>
      <div class="container-fluid common_con">
        <form class="form-horizontal article_form" id="form" ref="form">
          <div class="form-group">
            <label for="inputEmail1" class="col-sm-2 control-label"
              >用户名称：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="username"
                class="form-control username"
                id="inputEmail1"
                v-model="userInfo.username"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail2" class="col-sm-2 control-label"
              >昵称：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="nickname"
                class="form-control nickname"
                id="inputEmail2"
                v-model="userInfo.nickname"
              />
            </div>
          </div>
          <div class="form-group">
            <label for="inputEmail3" class="col-sm-2 control-label"
              >邮箱：</label
            >
            <div class="col-sm-4">
              <input
                type="text"
                name="email"
                class="form-control email"
                id="inputEmail3"
                v-model="userInfo.email"
              />
            </div>
          </div>

          <div class="form-group">
            <label for="inputPassword3" class="col-sm-2 control-label"
              >用户图标：</label
            >
            <div class="col-sm-10">
              <img :src="userInfo.userPic" alt="" class="user_pic" />
              <input
                style="display: block"
                name="userPic"
                type="file"
                id="exampleInputFile"
                @change="onChange"
                ref="file"
              />
              <!-- 给label标签的for属性赋值,值是input标签的id,那这样的话点击label就相当于点击了input -->
              <br />
              <label for="exampleInputFile">
                <img
                  src="@/assets/images/uploads_icon.jpg"
                  style="height: 50px;"
                  alt=""
                />
              </label>
            </div>
          </div>

          <div class="form-group">
            <label for="inputEmail4" class="col-sm-2 control-label"
              >密码：</label
            >
            <div class="col-sm-4">
              <input
                type="password"
                name="password"
                class="form-control password"
                id="inputEmail4"
                v-model="userInfo.password"
              />
            </div>
          </div>
          <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
              <button type="submit" class="btn btn-success btn-edit" @click.prevent="doEdit">
                修改
              </button>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入编辑接口
import {editUserInfo} from './user.js'
export default {
  name: 'user',
  data () {
    return {
      userInfo: {}
    }
  },
  methods: {
    onChange(){
        // 获取文件
        let file = this.$refs.file.files[0]
        // console.log(file);
        // 转成blob格式
        this.userInfo.userPic = URL.createObjectURL(file)
    },
    async doEdit(){
      let fd = new FormData(this.$refs.form)
      let res = await editUserInfo(fd)
      // console.log(res);
      this.$alert('修改成功')
      this.$store.commit('updateUserInfo',this.userInfo)
    }  
  },
  created () {
    this.userInfo = {
      ...this.$store.state.userInfo
    }
  }
}
</script>

<style></style>
